package com.guo.loadinganim;

import android.animation.Animator;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * 自定义Activity转场动效
 */
public class ActivityTransition extends Transition {
    private static final String TAG = "ActivityTransition";
    private static final String x = "x";
    private static final String y = "y";
    private static final String width = "width";
    private static final String height = "height";

    @Override
    public void captureStartValues(@NonNull TransitionValues transitionValues) {
        int[] loc = new int[2];
        transitionValues.view.getLocationOnScreen(loc);
        transitionValues.values.put(x, loc[0]);
        transitionValues.values.put(y, loc[1]);
        transitionValues.values.put(width, transitionValues.view.getWidth());
        transitionValues.values.put(height, transitionValues.view.getHeight());
        Log.d(TAG, "start view id = " + transitionValues.view.getId() + ",x = " + loc[0]);
    }

    @Override
    public void captureEndValues(@NonNull TransitionValues transitionValues) {
        int[] loc = new int[2];
        transitionValues.view.getLocationOnScreen(loc);
        transitionValues.values.put(x, loc[0]);
        transitionValues.values.put(y, loc[1]);
        transitionValues.values.put(width, transitionValues.view.getWidth());
        transitionValues.values.put(height, transitionValues.view.getHeight());
        Log.d(TAG, "end view id = " + transitionValues.view.getId() + ",x = " + loc[0]);
    }

    @Nullable
    @Override
    public Animator createAnimator(@NonNull ViewGroup sceneRoot, @Nullable TransitionValues startValues, @Nullable TransitionValues endValues) {
        if(null == startValues || null == endValues){
            return null;
        }
        int originX = (int)startValues.values.get(x);
        int originY = (int)startValues.values.get(y);
        int originWidth = (int)startValues.values.get(width);
        int originHeight = (int)startValues.values.get(height);
        int latestX = (int)endValues.values.get(x);
        int latestY = (int)endValues.values.get(y);
        int latestWidth = (int)endValues.values.get(width);
        int latestHeight = (int)endValues.values.get(height);
        View endView = endValues.view;
        endView.setPivotX(0.5f);
        endView.setPivotY(0.5f);
        endView.setTranslationX(originX - latestX);
        endView.setTranslationY(originY - latestY);
        endView.setScaleX(originWidth * 1.0f/ latestWidth);
        endView.setScaleY(originHeight * 1.0f / latestHeight);
        endView.animate().scaleX(1).scaleY(1).translationX(0).translationY(0).setDuration(500).setInterpolator(new LinearInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                Log.d(TAG, "transition start, view id = " + endView.getId());
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Log.d(TAG, "transition end, view id = " + endView.getId());
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        return super.createAnimator(sceneRoot, startValues, endValues);
    }
}
